package com.xbongbong.saas.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.pojo.dto.FormDataSummaryDTO;
import com.xbongbong.paas.pojo.vo.FormDataSummaryVO;
import com.xbongbong.paas.service.PaasFormDataService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.businessproduct.pojo.dto.BusinessProductListDTO;
import com.xbongbong.pro.detailtab.dto.DetailTabDataGetDTO;
import com.xbongbong.pro.detailtab.vo.ExpenseSummaryVO;
import com.xbongbong.saas.enums.RedundantTemplateTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ExpenseEnum;
import com.xbongbong.saas.service.BusinessExpenseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 业务费用service层接口
 * 创建时间 2020/4/14 4:45 PM
 * 修改时间 2020/4/14 4:45 PM
 * @author chy
 */
@Service("businessExpenseService")
public class BusinessExpenseServiceImpl implements BusinessExpenseService {

    private static final Logger LOG = LoggerFactory.getLogger(BusinessExpenseServiceImpl.class);
    @Resource
    private PaasFormDataService paasFormDataService;
    @Resource
    private PaasFormModel paasFormModel;

    @Override
    public ExpenseSummaryVO summaryExpenseList(DetailTabDataGetDTO detailTabDataGetDTO) throws XbbException {
        LOG.info("----------------summaryExpenseList--------2------");
        BusinessProductListDTO businessProductListDTO = new BusinessProductListDTO();
        BeanUtil.copyProperties(detailTabDataGetDTO, businessProductListDTO);
        // 详情页获取产品
        businessProductListDTO.setDetailGet(true);
        Integer businessType = businessProductListDTO.getBusinessType();
        RedundantTemplateTypeEnum templateTypeEnum = RedundantTemplateTypeEnum.getByCode(businessType);
        ExpenseSummaryVO expenseSummaryVO = new ExpenseSummaryVO();

        List<ConditionsEntityExt> conditionList = new ArrayList<>();
        ConditionsEntityExt conditionsEntityExt = null;
        switch (templateTypeEnum) {
            case CUSTOMER:
                conditionsEntityExt = new ConditionsEntityExt(ExpenseEnum.CUSTOMER_ID.getAttr(), "", ExpenseEnum.CUSTOMER_ID.getFieldType(), ConditionEnum.EQUAL.getSymbol(), Collections.singletonList(detailTabDataGetDTO.getDataId()));
                break;
            case SALES_OPPORTUNITY:
                conditionsEntityExt = new ConditionsEntityExt(ExpenseEnum.OPPORTUNITY_ID.getAttr(), "", ExpenseEnum.OPPORTUNITY_ID.getFieldType(), ConditionEnum.EQUAL.getSymbol(), Collections.singletonList(detailTabDataGetDTO.getDataId()));
                break;
            case CONTRACT:
                conditionsEntityExt = new ConditionsEntityExt(ExpenseEnum.CONTRACT_ID.getAttr(), "", ExpenseEnum.CONTRACT_ID.getFieldType(), ConditionEnum.EQUAL.getSymbol(), Collections.singletonList(detailTabDataGetDTO.getDataId()));
                break;
            default:
                break;
        }
        if (Objects.nonNull(conditionsEntityExt)) {
            conditionList.add(conditionsEntityExt);
        }

        FormDataSummaryDTO formDataSummaryDTO = new FormDataSummaryDTO();
        BeanUtil.copyProperties(detailTabDataGetDTO, formDataSummaryDTO);
        PaasFormEntityExt paasForm = paasFormModel.getByBusinessType(XbbRefTypeEnum.EXPENSE.getCode(), detailTabDataGetDTO.getCorpid());
        formDataSummaryDTO.setAppId(paasForm.getAppId());
        formDataSummaryDTO.setFormId(paasForm.getId());
        formDataSummaryDTO.setBusinessType(XbbRefTypeEnum.EXPENSE.getCode());
        formDataSummaryDTO.setSubBusinessType(XbbRefTypeEnum.EXPENSE.getCode());
        formDataSummaryDTO.setConditions(conditionList);
        LOG.info("formDataSummaryDTO:" + formDataSummaryDTO);
        FormDataSummaryVO formDataSummaryVO = paasFormDataService.getFormDataSummary(formDataSummaryDTO);
        JSONObject summary = formDataSummaryVO.getSummary();
        String apportionedAmountTotal = summary.getString(ExpenseEnum.APPORTIONED_AMOUNT.getAttr());
        apportionedAmountTotal = StringUtil.isEmpty(apportionedAmountTotal) ? "0.0" : apportionedAmountTotal;
        expenseSummaryVO.setSummaryInfo(String.format(I18nMessageUtil.getMessage(MessageConstant.APPORTIONED_AMOUNT_TOTAL), apportionedAmountTotal));
        return expenseSummaryVO;
    }

}
